package com.zego.wawaji.ui.center.pay.award;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.socket.utils.AnimatorUtil;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

/**
 * 首充大礼包
 * Created by Su on 2018/3/9.
 */
public class PayAwardActivity extends BaseAppCompatActivity implements View.OnClickListener, PObserver {
    private ObjectAnimator animator;
    private String content;

    private TextView btnOpen, tips_text;

    private ImageView awardImg;
    private RecyclerView recyclerView;
    private PayAwardAdapter adapter;

    private UserDetail userDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        isCanBack(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_award_activity);
        initView();
        initResultView();
    }

    private void initView() {
        setBackView();
        setTitle("首充大礼包");

        MsgMgr.getInstance().attach(this);
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        ImageView pay_award_head = (ImageView) findViewById(R.id.pay_award_head);
        String headUrl = ModuleMgr.getWawaMgr().getCommon().getFirstRechargeImg();
        if (!TextUtils.isEmpty(headUrl)) {
            ImageLoader.loadFitCenter(this, headUrl, pay_award_head);
        }
        awardImg = (ImageView) findViewById(R.id.award_img);
        awardImg.setOnClickListener(this);
        findViewById(R.id.btn_sign).setOnClickListener(this);
        tips_text = (TextView) findViewById(R.id.tips_text);
        btnOpen = (TextView) findViewById(R.id.btn_open);
        btnOpen.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.sign_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PayAwardAdapter(this);
        recyclerView.setAdapter(adapter);

        initBox();
    }

    private void initBox() {
        if (userDetail.getBoxStatus() == -1) { // 不可打开, 未首冲
            btnOpen.setText("立即充值");
            return;
        }
        Wawa wawa = ModuleMgr.getWawaMgr().getWawa(userDetail.getAwardWwId());
        if (userDetail.getBoxStatus() == 2) {
            resetBoxView(wawa);
        }
    }

    private void resetBoxView(Wawa wawa) {
        btnOpen.setBackground(null);
        btnOpen.setText("恭喜获得宝箱奖励：" + wawa.getName());
        btnOpen.setTextColor(getResources().getColor(R.color.text_black));
        awardImg.setImageResource(R.drawable.treasure_box02);
        tips_text.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.award_img:   // 开宝箱
            case R.id.btn_open:
                openBox();
                break;
            case R.id.btn_sign:
                if (userDetail.getBoxStatus() == -1) {
                    PToast.showShort("首充后才能开启领币哦");
                    return;
                }
                ModuleMgr.getWawaMgr().reqRewardWawaCoin(null);
                break;
        }
    }

    private boolean isOpening = false;

    /**
     * 开启宝箱
     */
    private void openBox() {
        if (userDetail.getBoxStatus() == -1) { // 不可打开, 未首冲
            PickerDialogUtil.showPayDialog(this);
            return;
        }

        if (userDetail.getBoxStatus() == 2) { // 已打开
            return;
        }

        if (userDetail.getBoxStatus() == 1) {  // 可打开
            if (!isOpening) {
                animBox();
            }
        }
    }

    private void animBox() {
        isOpening = true;
        ModuleMgr.getWawaMgr().reqLuckWawa(response -> {
            if (!response.isOk()) {
                isOpening = false;
                return;
            }
            wawa = new Wawa();
            wawa.parseJson(response.getResString());
            animator = AnimatorUtil.tada(awardImg);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.start();
            new Handler().postDelayed(() -> {
                animator.cancel();
                awardImg.setImageResource(R.drawable.treasure_box_anim);
                AnimationDrawable animationDrawable = (AnimationDrawable) awardImg.getDrawable();
                animationDrawable.start();
            }, 2000);

            new Handler().postDelayed(() -> {
                setShowAnimation(synth_suc);
                resetBoxView(wawa);
            }, 3200);
            MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
        });
    }

    // ================================== 开宝箱结果 ====================================
    private RelativeLayout synth_suc;
    private ImageView synth_img;
    private TextView synth_remark;
    private Wawa wawa;

    private void initResultView() {
        synth_suc = (RelativeLayout) findViewById(R.id.synth_suc);
        synth_img = (ImageView) findViewById(R.id.synth_img);
        synth_remark = (TextView) findViewById(R.id.synth_remark);
        synth_suc.setOnClickListener(this);   // 防点击透传

        findViewById(R.id.synth_close).setOnClickListener(v -> setHideAnimation(synth_suc));
    }

    /**
     * View渐隐动画效果
     */
    public void setHideAnimation(final View view) {
        if (view == null) return;

        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_fade_out));
        view.getAnimation().setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    /**
     * View渐现动画效果
     */
    public void setShowAnimation(final View view) {
        if (view == null) return;

        if (wawa != null) {
            ImageLoader.loadCenterCrop(this, wawa.getImg(), synth_img, 0, 0);
            synth_remark.setText("获得娃娃：" + wawa.getName());
        }

        view.setVisibility(View.INVISIBLE);
        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_fade_in));
        view.getAnimation().setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                userDetail = ModuleMgr.getCenterMgr().getMyInfo();
                if (adapter != null) {
                    adapter.refresh();
                }
                break;

            case MsgType.MT_Recharge_Suc:
                if (btnOpen != null)
                    btnOpen.setText("立即打开");
                break;
        }
    }
}
